package com.ljpz.manage.controller;

import com.ljpz.manage.pojo.Content;
import com.ljpz.manage.pojo.ContentCategory;
import com.ljpz.manage.service.ContentCategoryService;
import org.aspectj.apache.bcel.classfile.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 根据父节点查询对应的内容分类列表
 * Created by liujunchen on 2016/12/9.
 */
@RequestMapping("content/category")
@Controller
public class ContentCategoryController {
    @Autowired
    private  ContentCategoryService   contentCategoryService   ;
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<ContentCategory>>queryListByParentId(@RequestParam(value = "id",defaultValue = "0")Long pid ){
        try {
            ContentCategory record = new ContentCategory();
            record.setParentId(pid);
            List<ContentCategory> list = this.contentCategoryService.queryListByWhere(record);
            if (null == list || list.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(list);
        } catch (Exception e ){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     *
     * 新增节点
     * @param contentCategory
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ContentCategory>saveContentCategory(ContentCategory contentCategory){
        try {
            /*contentCategory.setIsParent(false);
            contentCategory.setId(null);
            contentCategory.setSortOrder(1);
            contentCategory.setStatus(1);*/
            this.contentCategoryService.saveContentCategory(contentCategory);
            /*//判断该节点的父节点的isparent是否为true ， 不是需要修改为true
            ContentCategory  parent= this.contentCategoryService.queryById(contentCategory.getParentId()) ;
            if(!parent.getIsParent()){
                parent.setIsParent(true);
                this.contentCategoryService.update(parent)  ;}*/
            return  ResponseEntity.status(HttpStatus.CREATED).body(contentCategory) ;
        }catch (Exception e ){
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null)  ;
    }

    /**
     *
     * 根据id来重新命名
     * @param id
     * @param name
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public  ResponseEntity<Void> rename(@RequestParam("id") Long id  , @RequestParam("name")String name ){
        try {
            ContentCategory contentCategory = new ContentCategory();
            contentCategory.setId(id);
            contentCategory.setName(name);
            this.contentCategoryService.updateSelective(contentCategory);
            return  ResponseEntity.status(HttpStatus.NO_CONTENT).build() ;
        }catch (Exception e ){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     *
     * 删除节点， 包含它的所有子节点都被删除
     * @param contentCategory
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public  ResponseEntity<Void>delete(ContentCategory contentCategory){
        try {
            this.contentCategoryService.deleteAll(contentCategory);
            return    ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e ){
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
